/***************************************************
 @Name :        小铭同学
 @Time :        2025-09-03
 @Email :       LiaoMingWJ233@163.com
 @Comment :     LED灯 窗口
 @FileName :    led.cpp
 @Version :     1.0
****************************************************/


#include "led.h"
#include "ui_led.h"

LED::LED(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LED)
{
    ui->setupUi(this);
    
    // 使用单例模式获取TCP实例
    tcp =  TCP::instance();
    
    led_control = new ledDevice;
    
    ui->label_zt->setText("LED");
    ui->label_zt->setStyleSheet("QLabel {"
        "    font-size: 50px;"           // 字体大小
        "    color: #87cefa;"            // 文字颜色
        "    font-weight: bold;"         // 字体粗细
        "    font-family: '楷体';"        // 字体家族
        "}"
    );
    
    // LED状态初始化
    led_initial(ui->pushButton_led1);
    led_initial(ui->pushButton_led2);
    led_initial(ui->pushButton_led3);
    led_initial(ui->pushButton_led4);
    led_initial(ui->pushButton_led5);
    led_initial(ui->pushButton_led6);
    led_initial(ui->pushButton_led_allg);
    led_initial(ui->pushButton_led_allk);
    
    // 连接信号与槽
    connect(ui->pushButton_led1, &QPushButton::clicked, this, &LED::led1_state);
    connect(ui->pushButton_led2, &QPushButton::clicked, this, &LED::led2_state);
    connect(ui->pushButton_led3, &QPushButton::clicked, this, &LED::led3_state);
    connect(ui->pushButton_led4, &QPushButton::clicked, this, &LED::led4_state);
    connect(ui->pushButton_led5, &QPushButton::clicked, this, &LED::led5_state);
    connect(ui->pushButton_led6, &QPushButton::clicked, this, &LED::led6_state);
    connect(ui->pushButton_led_allg, &QPushButton::clicked, this, &LED::ledallg_state);
    connect(ui->pushButton_led_allk, &QPushButton::clicked, this, &LED::ledallk_state);
    
}

LED::~LED()
{
    delete ui;
    // 注意：不要删除tcp，因为它是单例对象，由单例类自己管理
    delete led_control;
}

void LED::led_initial(QPushButton *led)
{
    led->setText("");
    led->setFlat(true);
    led->setIcon(QIcon(":/关灯"));      
    led->setIconSize(QSize(120, 120));
}

// LED1状态控制
void LED::led1_state()
{
    led1_State = !led1_State;  // 切换状态
    
    if (led1_State) {
        led_control->led_On(led1);
        ui->pushButton_led1->setIcon(QIcon(":/开灯"));  // 开灯图标
    } else {
        led_control->led_Off(led1);
        ui->pushButton_led1->setIcon(QIcon(":/关灯"));  // 关灯图标
    }
}

// LED2状态控制
void LED::led2_state()
{
    led2_State = !led2_State;
    
    if (led2_State) {
        led_control->led_On(led2);
        ui->pushButton_led2->setIcon(QIcon(":/开灯"));
    } else {
        led_control->led_Off(led2);
        ui->pushButton_led2->setIcon(QIcon(":/关灯"));
    }
}

// LED3状态控制
void LED::led3_state()
{
    led3_State = !led3_State;
    
    if (led3_State) {
        led_control->led_On(led3);
        ui->pushButton_led3->setIcon(QIcon(":/开灯"));
    } else {
        led_control->led_Off(led3);
        ui->pushButton_led3->setIcon(QIcon(":/关灯"));
    }
}

// LED4状态控制
void LED::led4_state()
{
    led4_State = !led4_State;
    
    if (led4_State) {
        led_control->led_On(led4);
        ui->pushButton_led4->setIcon(QIcon(":/开灯"));
    } else {
        led_control->led_Off(led4);
        ui->pushButton_led4->setIcon(QIcon(":/关灯"));
    }
}

// LED5状态控制
void LED::led5_state()
{
    led5_State = !led5_State;
    
    if (led5_State) {
        led_control->led_On(led5);
        ui->pushButton_led5->setIcon(QIcon(":/开灯"));
    } else {
        led_control->led_Off(led5);
        ui->pushButton_led5->setIcon(QIcon(":/关灯"));
    }
}

// LED6状态控制
void LED::led6_state()
{
    led6_State = !led6_State;
    
    if (led6_State) {
        tcp->onSendButtonClicked("LED_D2k");  // 使用单例方法发送消息
        ui->pushButton_led6->setIcon(QIcon(":/开灯"));
    } else {
        tcp->onSendButtonClicked("LED_D2g");  // 使用单例方法发送消息
        ui->pushButton_led6->setIcon(QIcon(":/关灯"));
    }
}

// 全部LED开
void LED::ledallg_state()
{
    ledallg_State = !ledallg_State;
    
    if (ledallg_State) {
        // 打开所有LED
        led_control->led_On(led1);
        led_control->led_On(led2);
        led_control->led_On(led3);
        led_control->led_On(led4);
        led_control->led_On(led5);
        tcp->onSendButtonClicked("LED_D2k");  // 使用单例方法发送消息
        
        // 更新所有按钮图标
        ui->pushButton_led1->setIcon(QIcon(":/开灯"));
        ui->pushButton_led2->setIcon(QIcon(":/开灯"));
        ui->pushButton_led3->setIcon(QIcon(":/开灯"));
        ui->pushButton_led4->setIcon(QIcon(":/开灯"));
        ui->pushButton_led5->setIcon(QIcon(":/开灯"));
        ui->pushButton_led6->setIcon(QIcon(":/开灯"));
        ui->pushButton_led_allg->setIcon(QIcon(":/开灯"));
        
        // 更新所有状态变量
        led1_State = true;
        led2_State = true;
        led3_State = true;
        led4_State = true;
        led5_State = true;
        led6_State = true;
        ledallk_State = false; // 关闭全关状态
    } else {
        // 关闭所有LED
        led_control->led_Off(led1);
        led_control->led_Off(led2);
        led_control->led_Off(led3);
        led_control->led_Off(led4);
        led_control->led_Off(led5);
        tcp->onSendButtonClicked("LED_D2g");  // 使用单例方法发送消息
        
        // 更新所有按钮图标
        ui->pushButton_led1->setIcon(QIcon(":/关灯"));
        ui->pushButton_led2->setIcon(QIcon(":/关灯"));
        ui->pushButton_led3->setIcon(QIcon(":/关灯"));
        ui->pushButton_led4->setIcon(QIcon(":/关灯"));
        ui->pushButton_led5->setIcon(QIcon(":/关灯"));
        ui->pushButton_led6->setIcon(QIcon(":/关灯"));
        ui->pushButton_led_allg->setIcon(QIcon(":/关灯"));
        
        // 更新所有状态变量
        led1_State = false;
        led2_State = false;
        led3_State = false;
        led4_State = false;
        led5_State = false;
        led6_State = false;
    }
}

// 全部LED关
void LED::ledallk_state()
{
    ledallk_State = !ledallk_State;
    
    if (ledallk_State) {
        // 关闭所有LED
        led_control->led_Off(led1);
        led_control->led_Off(led2);
        led_control->led_Off(led3);
        led_control->led_Off(led4);
        led_control->led_Off(led5);
        tcp->onSendButtonClicked("LED_D2g");  // 使用单例方法发送消息
        
        // 更新所有按钮图标
        ui->pushButton_led1->setIcon(QIcon(":/关灯"));
        ui->pushButton_led2->setIcon(QIcon(":/关灯"));
        ui->pushButton_led3->setIcon(QIcon(":/关灯"));
        ui->pushButton_led4->setIcon(QIcon(":/关灯"));
        ui->pushButton_led5->setIcon(QIcon(":/关灯"));
        ui->pushButton_led6->setIcon(QIcon(":/关灯"));
        ui->pushButton_led_allk->setIcon(QIcon(":/关灯"));
        
        // 更新所有状态变量
        led1_State = false;
        led2_State = false;
        led3_State = false;
        led4_State = false;
        led5_State = false;
        led6_State = false;
        ledallg_State = false; // 关闭全开状态
    } else {
        // 这里可以根据需要实现其他逻辑，或者保持关闭状态
        ui->pushButton_led_allk->setIcon(QIcon(":/关灯"));
    }
}
